package com.whenjun.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ifootbook.util.common.BackJson;
import com.ifootbook.util.common.CallResult;
import com.whenjun.common.LoggerParent;
import com.whenjun.dto.account.*;
import com.whenjun.service.core.AccountUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: shirley
 * @Date: 2023/7/24 22:36
 * @Description:
 */
@RequestMapping("/account")
@RestController
@SaCheckPermission(value = "account", orRole = "admin")
@Api(tags = "账号管理")
@ApiImplicitParams({
        @ApiImplicitParam(name="token",value="token",paramType="header",required=false,dataType="String",dataTypeClass = String.class)
})
public class AccountController extends LoggerParent {

    @Autowired
    private AccountUserService accountUserService;

    @PostMapping("accountList")
    @ApiOperation("账号列表")
    public CallResult<IPage<AccountResp>> accountList(@RequestBody AccountListReq accountListReq){
        return BackJson.success(accountUserService.accountList(accountListReq));
    }

    @PostMapping("getInitPermission")
    @ApiOperation("获取初始化权限数据（用于新增用户时携带的权限配置）")
    public CallResult<List<AuthPermissionDTO>> getInitPermission(){
        return BackJson.success(accountUserService.getInitPermission());
    }

    @PostMapping("accountAdd")
    @ApiOperation("创建用户")
    public CallResult<?> accountAdd(@Valid @RequestBody AccountAddReq accountAddReq){
        accountUserService.accountAdd(accountAddReq);
        return BackJson.success();
    }

    @PostMapping("accountResetPassword")
    @ApiOperation("重置用户密码")
    public CallResult<?> accountResetPassword(@Valid @RequestBody AccountResetPasswordReq accountResetPasswordReq){
        accountUserService.accountResetPassword(accountResetPasswordReq);
        return BackJson.success();
    }

    @PostMapping("accountDel")
    @ApiOperation("删除用户")
    public CallResult<?> accountDel(@Valid @RequestBody AccountDelReq accountDelReq){
        accountUserService.accountDel(accountDelReq);
        return BackJson.success();
    }


    @PostMapping("accountUpd")
    @ApiOperation("更新用户权限、重置用户密码、更新用户登陆名称或昵称")
    public CallResult<?> accountUpd(@Valid @RequestBody AccountUpd accountUpd){
        accountUserService.accountUpd(accountUpd);
        return BackJson.success();
    }
}
